import {
  home
} from '../../../../api/index'
import {
  formatTime
} from "../../../../utils/util";
const app = getApp()
Component({
  properties: {
    detailId: "",
  },

  // 这里是一些组件内部数据
  data: {
    topDistance: "",
    topDistance_2: "",
    bottomDistance: "",
    PageIndex: 1,
    PageSize: 10,
    loading: false, //是否正在加载数据
    ID: "",
    commentList: [],
    TotalCount: 1,
    detailId: ''
  },
  lifetimes: {
    ready: function () {
      this.getData()
    },

  },

  methods: {
    // 获取数据
    async getData() {
      // 如果正在加载数据，则直接返回，避免重复加载
      if (this.data.loading) {
        return;
      }
      // 设置加载中的标志
      this.setData({
        loading: true,
      });
      if (this.data.commentList.length === this.data.TotalCount) {
        return
      }
      const {
        PageIndex,
        PageSize,
        ID
      } = this.data
      let res = await home.commentsortbyhot({
        PageIndex,
        PageSize,
        ID: this.properties.detailId
      })
      res.Result.Datas.forEach(item => {
        // 设置每个一级评论下的二级评论数据的初始状态
        item.showSecondary = false
        item.secondaryCommentList = []
      })
      console.log(res.Result.Datas, '一级评论的数组')
      this.setData({
        commentList: this.data.commentList.concat(res.Result.Datas),
        TotalCount: res.Result.TotalCount,
        PageIndex: this.data.PageIndex + 1,
        loading: false
      })

    },
    // 给一级评论点赞
    Ilike(e) {
      // console.log(e.currentTarget.dataset)
      const _this = this
      const ID = e.currentTarget.dataset.id
      const IsLike = e.currentTarget.dataset.islike
      let index = e.currentTarget.dataset.index //一级评论的index
      const obj = {
        LikeID: ID
      }
      home[IsLike ? 'removelikeformcomment' : 'addlikeformcomment'](obj).then((res) => {
        if (res.StatusCode === 200) {
          const num = IsLike ? this.data.commentList[index].LikeCount -= 1 : this.data.commentList[index].LikeCount += 1
          let comment = this.data.commentList[index];
          comment.IsLike = !comment.IsLike
          comment.LikeCount = num
          this.setData({
            commentList: this.data.commentList
          })
        }
      }).catch(err => {
        if (err.statusCode === 401) {
          // 未登录
          _this.shudLogin();
        }
      })
    },
    // 展开、收起操作
    async toggleShowSecondary(e) {
      const {
        index,
        id
      } = e.currentTarget.dataset
      let correctComment = this.data.commentList[index]
      correctComment.showSecondary = !correctComment.showSecondary
      if (correctComment.showSecondary) {
        // 获取二级评论
        let res = await home.getsecondcomment({
          "PageIndex": 1,
          "PageSize": 100,
          "ParentId": id
        })
        correctComment.secondaryCommentList = res.Result.Datas
      }
      this.setData({
        commentList: this.data.commentList
      })
      // console.log(this.data.commentList,'设置后的commentList')
    },
    // 给二级评论点赞
    secondaryLike(e) {
      // index:一级评论的下标，secondaryindex：一级评论下二级评论的下标  islike：二级评论是否点赞 id：二级评论的id
      const {
        index,
        islike,
        secondaryindex,
        id
      } = e.currentTarget.dataset
      const obj = {
        LikeID: id
      }
      home[islike ? 'removelikeformcomment' : 'addlikeformcomment'](obj).then((res) => {
        if (res.StatusCode === 200) {
          const num = islike ? this.data.commentList[index].secondaryCommentList[secondaryindex].LikeCount -= 1 : this.data.commentList[index].secondaryCommentList[secondaryindex].LikeCount += 1
          let comment = this.data.commentList[index].secondaryCommentList[secondaryindex];
          comment.IsLike = !comment.IsLike
          comment.LikeCount = num
          this.setData({
            commentList: this.data.commentList
          })
        }
      }).catch(err => {
        if (err.statusCode === 401) {
          // 未登录
          _this.shudLogin();
        }
      })
    },
    //去登录
    shudLogin() {
      const popup = this.selectComponent('#popupid');
      popup.setData({
        visible: true
      })
    },

    //预览风格大图片
    PreviewPicture(e) {
      wx.previewImage({
        current: e.currentTarget.dataset.atpresentimg, // 当前显示图片的http链接
        urls: e.currentTarget.dataset.imglist, // 需要预览的图片http链接列表
      });
    },
  },

  onReachBottom() {
    console.log('滚动位置:', e.detail.offset);
    this.getData()
  },


})